<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目名称" class="row-padding-bottom">
              <el-input
                type="text"
                placeholder="请填写项目完整名称"
                v-model="filterField.projectName"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目状态" class="row-padding-bottom">
              <el-select
                v-model="filterField.projectStatus"
                filterable
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in statusOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="地区" class="row-padding-bottom">
                <el-cascader
                  ref="manageArea"
                  v-model="areaId"
                  :options="areasOptions"
                  @change="getAreaId"
                  clearable
                  filterable
                  placeholder="请选择"
                  :props="{ checkStrictly: true }"
                  :append-to-body="false"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="受理机构" class="row-padding-bottom">
                <el-select
                  v-model="filterField.bureau"
                  filterable
                  placeholder="请选择"
                  clearable
                  no-data-text="请先选择区域"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in bureaus"
                    :key="item.bureau"
                    :label="item.bureau"
                    :value="item.bureau"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="审核状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.status"
                  filterable
                  placeholder="请选择"
                  clearable
                  no-data-text="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in statusList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- <el-form-item>
                        <el-button type="primary" icon="icon iconfont iconchazhao1" @click="searchList" class="commonBtn"></el-button>
                    </el-form-item> -->
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
            <el-form-item>
              <exportBtn
                :dataLen="tableList.length"
                :filterField="filterField"
                api="api-e/cs/ins/expand/monitor/export"
                responseType="get"
              ></exportBtn>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          @sort-change="sortChange"
          style="border: 1px solid #dfe6ec"
        >
          <!-- <el-table-column type="selection" width="55"/> -->
          <el-table-column
            prop="projectName"
            label="项目名称"
            width="230"
            fixed="left"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="projectStatusDesc"
            label="项目状态"
            :formatter="formatTd"
            width="100"
          />
          <el-table-column
            prop="areaName"
            label="区域"
            :formatter="formatTd"
            width="230"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.areaName"
                :columnName="'areaName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="companyName"
            label="参保单位"
            :formatter="formatTd"
            width="230"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.companyName"
                :columnName="'companyName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="insNo"
            label="单位编号"
            :formatter="formatTd"
            width="230"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.insNo"
                :columnName="'insNo'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="bureau"
            label="参保受理机构"
            :formatter="formatTd"
            min-width="240"
          />
          <el-table-column
            prop="insDate"
            label="参保时间"
            sortable="custom"
            :formatter="formatTd"
            min-width="160"
          />
          <el-table-column
            prop="files"
            label="参保凭证"
            :formatter="formatTd"
            min-width="140"
          >
            <template slot-scope="scope">
              <span
                class="seeFile"
                @click="uploadFile(scope.row)"
                v-if="scope.row.fileList"
                >查看</span
              >
              <span v-else>--</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="statusDesc"
            label="状态"
            :formatter="formatTd"
            min-width="140"
          />
          <el-table-column
            prop="operatorName"
            label="审核人"
            :formatter="formatTd"
            min-width="140"
          />
          <el-table-column
            prop="operationDate"
            label="审核时间"
            sortable="custom"
            :formatter="formatTd"
            min-width="140"
          />
          <el-table-column
            prop="insNo"
            label="驳回原因"
            :formatter="formatTd"
            width="230"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.reason"
                :columnName="'reason'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="left"
            fixed="right"
            width="160"
            :formatter="formatTd"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.status == 'AUDIT'">
                <el-link type="primary" @click="toPass(scope.row)"
                  >审核通过</el-link
                >
                <el-link type="primary" @click="toReject(scope.row)"
                  >驳回</el-link
                >
              </span>
              <span v-else>--</span>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关工伤保险数据"></emptyTable>
          </div>
        </el-table>

        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
        <!-- 合同附件 -->
        <contractEnclosureDialog
          v-if="uploadContract"
          :dialogTitle="title"
          :tableData="tableData"
          @closeDialog="hiddenDialog"
        ></contractEnclosureDialog>
        <!-- 驳回申报弹框 -->
        <el-dialog
          title="驳回申报"
          :visible.sync="formVisibleReject"
          :close-on-click-modal="false"
          @close="closeDialog"
          custom-class="brand-dialog-min-width"
          width="420px"
          :modal-append-to-body="false"
          class="tipDialog"
        >
          <el-form
            :model="formDataReject"
            label-width="80px"
            :rules="formRulesReject"
            ref="formDataReject"
          >
            <el-form-item
              label="驳回原因"
              class="row-padding-bottom"
              prop="reason"
            >
              <el-input
                type="textarea"
                rows="3"
                v-model="formDataReject.reason"
                @blur="formDataReject.reason = changeValue($event)"
                maxlength="100"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click.native="handleClose" class="cancel"
              >取消</el-button
            >
            <el-button
              type="primary"
              @click.native="submitRejectSave"
              v-sdc
              class="save"
              >保存</el-button
            >
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'
import { mapActions, mapState } from 'vuex'
export default {
  name: 'insuranceCheck',
  mixins: [scrollTable],
  data() {
    return {
      // activeName: "1",
      titleText: '工伤保险审核',
      statusList: [],
      advanced: false,
      areaId: '',
      bureaus: [],
      //表格数据
      tableList: [],
      requestUrl: 'api-e/cs/ins/expand/monitor/list',
      //搜索条件
      filterField: {
        projectName: '',
        bureau: '',
        projectStatus: '',
        areaId: '',
        status: '',
      },
      isShow: false, //列表加载
      loading: false,
      tableData: [],
      uploadContract: false,
      title: '参保凭证',
      formVisibleReject: false,
      rowId: '',
      formDataReject: {
        reason: '',
      },
      formRulesReject: {
        reason: [
          { required: true, message: '请输入驳回原因', trigger: 'blur' },
          { max: 100, message: '需填写100字以内的驳回原因', trigger: 'blur' },
        ],
      },
    }
  },
  computed: { ...mapState(['areasOptions', 'statusOptions']) },
  created() {
    this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
    this.getStatusList()
    if (this.$route.query.projectName) {
      this.filterField.projectName = this.$route.query.projectName
    }
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    // this.isShowDetail = this.$store.state.buttonLimitsArr.indexOf('mInsurance:detail') > -1 ;
  },
  methods: {
    //tabs栏切换
    // handleClick(tab, event) {
    //     this.activeName = tab.name
    // },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    ...mapActions(['getAreas']),
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
      this.getBureauData()
    },
    getBureauData() {
      this.bureaus = []
      this.filterField.bureau = ''
      this.axios({
        method: 'post',
        url: 'api-e/cs/insstats/expand/monitor/selectarea',
        data: { areaId: this.filterField.areaId },
      })
        .then((resp) => {
          if (!resp.data.hasError) {
            this.bureaus = resp.data.result
          } else {
            this.$message({
              showClose: true,
              duration: 3000,
              message: resp.data.errorMsg,
              type: 'error',
            })
          }
        })
        .catch((resp) => {
          this.$message.error('数据获取失败')
        })
    },
    getStatusList() {
      this.axios({
        method: 'post',
        url: 'api-e/cs/ins/expand/status',
      })
        .then((resp) => {
          if (!resp.data.hasError) {
            this.statusList = resp.data.result
          } else {
            this.$message({
              showClose: true,
              duration: 3000,
              message: resp.data.errorMsg,
              type: 'error',
            })
          }
        })
        .catch((resp) => {
          this.$message.error('数据获取失败')
        })
    },
    toReject(row) {
      this.rowId = row.id
      this.formVisibleReject = true
    },
    handleClose() {
      this.rowId = ''
      this.$refs.formDataReject.resetFields()
      this.formVisibleReject = false
    },
    //驳回保存
    submitRejectSave() {
      this.$refs.formDataReject.validate((valid) => {
        if (valid) {
          this.editstatus('REJECT')
        } else {
          return false
        }
      })
    },
    editstatus(status) {
      this.axios({
        method: 'post',
        url: 'api-e/cs/ins/expand/editstatus',
        data: {
          id: this.rowId,
          status: status,
          reason: this.formDataReject.reason,
        },
      })
        .then((resp) => {
          if (!resp.data.hasError) {
            var text = ''
            if (status == 'REJECT') {
              text = '已驳回'
            } else {
              text = '审核通过'
            }
            this.formVisibleReject = false
            this.$message({
              message: text,
              type: 'success',
            })
            this.$nextTick(function () {
              this.$refs.pagination.getTableListData()
            })
          } else {
            this.$message({
              showClose: true,
              duration: 3000,
              message: resp.data.errorMsg,
              type: 'error',
            })
          }
        })
        .catch((resp) => {
          this.$message.error('数据获取失败')
        })
    },
    toPass(row) {
      this.$confirm('确认审核通过吗?', '提示', {
        cancelButtonClass: 'btn-custom-cancel',
        type: 'warning',
      })
        .then(() => {
          this.rowId = row.id
          this.editstatus('APPROVED')
        })
        .catch(() => {
          // // this.$message.error("删除失败");
        })
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    toDetail(row) {
      console.log(row)
    },
    handleSelectionChange(val) {
      console.log(val)
    },
    uploadFile(row) {
      this.tableData = row.fileList
      this.uploadContract = true
    },
    hiddenDialog() {
      this.uploadContract = false
    },
  },
}
</script>

<style scoped lang="less">
.mainCon {
  top: 198px;
  padding: 24px 0 0 0;
  &Header {
    position: relative;
    &Content {
      position: absolute;
      left: 130px;
      z-index: 100;
      font-size: 12px;
      color: #666666;
    }
  }
}

@media only screen and (max-width: 1366px) {
  /deep/.searchInputWidth .el-form--inline .el-form-item__label {
    width: 80px;
  }
}
@import '../../../static/css/filterArea.less';

// tab栏切换
.tabs {
  /deep/ .el-tabs__content {
    background: #fff;
    padding: 0px;
  }
  /deep/ .el-tabs__header {
    margin: 0;
  }
  /deep/ .el-tabs__nav-scroll {
    overflow: hidden;
    background: #e4ebf1;
    /* margin-top: 10px; */
    padding-top: 15px;
  }
  /deep/ .el-tabs__item.is-active {
    font-size: 14px;
    color: #333;
    font-weight: bold;
    background: #fff;
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
  }
  /deep/ .el-tabs__item {
    color: #333;
    background: #d5e0e9;
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    font-weight: normal;
  }
  /deep/ .el-tabs__item:first-child {
    margin: 0 4px 0px 24px;
  }
  /deep/ .el-tabs__item {
    margin-right: 4px;
  }
  /deep/ .el-tabs__item:last-child {
    margin-right: 0;
  }
}
</style>
